var Redirect = new Class({
    Implements:Options,
    options:{
        data: null,
        isShowNavigator: "false",
        items: []//这个items具体是[{title:'a页面',page:'a.html',iframe:'xxx',bodyScroll:null},{title:'b页面',page:'b.html',iframe:'xxx',bodyScroll:null}]
    },
    topDiv: null,
    initialize:function() {

    },
    go:function(page) {
        var bodyScroll = $(document.body).getScroll();
        bodyScroll = (bodyScroll.x == 0 && bodyScroll.y == 0) ? null : bodyScroll;
        var iframeNode;
        var parentDoc ;
        try{
        	parentDoc = $(parent.document);
        }catch(e){}
        var topDivs;
        if ($defined(parentDoc)) {
            topDivs = parentDoc.getElements('div[name=SwordRedirect_topDiv]');
        }
        if (parent && $defined(topDivs) && topDivs.length != 0) {
            var title = (document.title == null) ? "空title页面" : document.title;
            parent.pc.redirect.options.items[parent.pc.redirect.options.items.length - 1].title = title;
            parent.pc.redirect.options.items[parent.pc.redirect.options.items.length - 1].bodyScroll = bodyScroll;
            this.options.items = parent.pc.redirect.options.items;
            this.options.items[this.options.items.length - 1].iframe.setStyles({
                'display': 'none'
            });

            iframeNode = parent.document.createElement('iframe');
            iframeNode.frameBorder = "no";
            $(iframeNode).inject(parent.document.body);
        } else {
            if ($(document).getElements('div[name=SwordRedirect_topDiv]').length != 0) {
                this.topDiv.setStyles({
                    'display': 'none'
                });
            } else {
                this.topDiv = new Element('div', {
                    'name': 'SwordRedirect_topDiv',
                    'styles':{
                        'display':'none'
                    }
                }).inject(document.body, 'top');
                $$("body>*[name!=SwordRedirect_topDiv]").each(function(item) {
                    this.topDiv.wraps(item);
                }, this);
                
                var title = (document.title == null) ? "空title页面" : document.title;
	            this.options.items.include({
	                'title': title,
	                'page': null,
	                'iframe': this.topDiv,
	                'bodyScroll': bodyScroll
	            });
            	
            }

            iframeNode = document.createElement('iframe');
            iframeNode.frameBorder = "no";//todo 这句话是ie不出现边框而设置的
            $(iframeNode).inject(document.body);
        }

        if (jsR.server()) {
            //服务器运行时候：为了保证页面引用js路径一致
            $(iframeNode).setProperty('src', jsR.rootPath + 'sword?ctrl=SwordPage_redirect&pagename=' + page);
        } else {
            //静态运行:
            $(iframeNode).setProperty('src', page);
        }

        $(iframeNode).setProperty('name', 'RedirectIframe_' + page);

        $(iframeNode).set('ifname', 'RedirectIframe_' + page);//使用这个ifname属性，来作为唯一标识符

        $(iframeNode).setProperty('frameborder', 'no');
        $(iframeNode).setStyles({
            'display': "",
            'width': '100%',
            'height': '100%',
            'overflow': 'auto',
            'border': 0
        });
        //下面是解决滚动条的问题的脚本,暂时的解决办法是设置iframe中的body高度,使其没有iframe中的高,有更好的解决办法,请修改
        $(iframeNode).addEvent('load', function() {
            if ($defined(window.frames['RedirectIframe_' + page])) {
                $(window.frames[window.frames.length - 1].document.body).setStyles({
                    'height': '100%'
                });
            } else {
            	 if($(parent.frames[parent.frames.length - 1].document.body))$(parent.frames[parent.frames.length - 1].document.body).setStyles({
                    'height': '100%'
                });
            }

        }.bind(this));

        this.options.items.include({
            'title': null,
            'page': page,
            'iframe': $(iframeNode),
            'bodyScroll': null
        });

    },
    /*back: function(page) {
        parent.pc.redirect.ItemBack(page);
    },*/
    hidePEl:function(){ //隐藏父页面的非 swordStateFormIframe元素。
//    	$(self.document.body).setStyle("padding","0");
    	if(!this.topHideDiv){
    		this.topHideDiv = new Element('div', {
    			'name': 'hideParentElDiv',
                    'styles':{
                        'display':'none'
                    }
    		});
    		//todo 此处使用原生的元素集合是为了避免body中直接出现文本节点，导致的错误。
    		var elA=document.body.childNodes; 
    		for(var i=elA.length-1;i>=0;i--){
    			var el=elA[i];
    			if(el.hasClass&&(el.hasClass("maskDivEl")||el.hasClass("loading9")))continue;
    			if(el.id=="_persist_flash_wrap")continue;
    			this.topHideDiv.grab(el,'top');
    		}
    		this.topHideDiv.inject(document.body, 'top');
    		//this.topHideDiv.inject(document.body, 'top');
    		/*$$("body>*[myid!=swordStateFormIframe]").each(function(item,index) {
    			if(index==0||item.hasClass("maskDivEl")||item.hasClass("loading9"))return;
    			this.topHideDiv.wraps(item);
    		}, this);*/
    	}else{
    		this.topHideDiv.setStyle("display","none");
    	}
    },
    back: function(page) {
    	var pc=self.parent.pc;
        var hideDiv=pc.redirect.topHideDiv;
           if(hideDiv&&hideDiv.innerHTML!=""){
                var pBody=self.parent.document.body;
//                pBody.setStyle("padding","0 30px");
                hideDiv.setStyle("display","");
                var tempForm=pBody.getElement("form[name='swordStateForm']");
                var tempIframe=pBody.getElement("iframe[myid='swordStateFormIframe']");
                pc.stateForm=null;
                if(tempIframe)tempIframe.setStyle('display','none');
                //解决：ie9提交之后，onsuccess里面调用pc.redirect.back会报$defined未定义
                this.clearIframe.delay(101,this,[tempForm,tempIframe]);
           }
           this.goScroll.delay(100,this);
    },
    goScroll:function(){ 
    	parent.document.documentElement.scrollTop =parent.pc.scY;
    },
    clearIframe:function(tempForm,tempIframe){
    	if(tempForm)tempForm.destroy();
        if(tempIframe){
        	tempIframe.src='';
        	tempIframe.destroy();
        }
    }
    ,
    ItemBack:function(page) {
        var backFlag = false;
        var bodyScroll;
        var backIndex = 0;
        var last=this.options.items.pop();
        last.iframe.destroy();
        var tempArray=this.options.items;
        tempArray[tempArray.length-1].iframe.setStyles({'display': 'block'});;
       
        if ($defined(bodyScroll)) {
            $(document.body).scrollTo(bodyScroll.x, bodyScroll.y);
        }

    },
    getData:function() {
        return this.options.data;
    },
    setData:function(data) {
        this.options.data = data;
    }
});


